/**
 * @description 游戏资源加载完成以后进入的主界面
 * 1. 页面调用
 * 2. 背景音乐&&点击特效引用调用
 */
class myHome extends eui.Component{
    private maskBg:egret.Shape;
    private isInvite = false;
    private notice:eui.Image;
    private open = true;
    private load = false;//植物加载完成
    //页面元素初始化
    private headMask:eui.Image;
    private head:eui.Image;
    private nickName:eui.Label;
    private title:eui.Label;
    private project:eui.Rect;
    private exp:eui.Label;
    private energy:eui.Label;
    private interaction:eui.Image;
    private born:eui.Label;
    private bornMask:eui.Rect;
    private bornGroup:eui.Group;

    private area:eui.Group;
    private topGroup:eui.Group;
    private bottomGroup:eui.Group;
    private addEnergy:eui.Image;
    private friend:eui.Button;
    private invite:eui.Button;
    private logistics:eui.Button;
    private ranking:eui.Button;
    private welfare:eui.Button;
    private mall:eui.Button;
    private warehouse:eui.Button;
    private help:eui.Button;
    private pointHelp:eui.Image;
    //指导界面元素
    private guideGroup:eui.Group;
    private welcome:eui.Group;
    private guideToShow:eui.Group;
    private guideToPlant:eui.Group;
    private toPlant:eui.Rect;
    private guideToDetails:eui.Group;
    private toDetails:eui.Rect;
    private showPoint:eui.Image;
    private plantPoint:eui.Image;
    private detailsPoint:eui.Image;
    private stop:eui.Image;
    private toDoPlant:eui.Rect;

    //种植框
    private plantFrame:eui.Group;
    private indexId:number = -1;//土地id索引

    private toast:Toast = new Toast();
    private _model:model;

    private mailbox:eui.Image;
    private plantIndex:Array<any> = [{x:333,y:701},{x:236,y:748},{x:431,y:748},{
        x:144,y:798},{x:333,y:798},{x:525,y:798},{x:236,y:848},{x:431,y:848},{x:333,y:897}];
    private plant:Array<plantInfom> = [];//植物种植

    private seedScroller:eui.Scroller;
    private _seedArray:eui.ArrayCollection;
    private seedList:eui.List;
    private _seedData:Array<any> = [];
   
    //其他页面初始化
    private _uiFocused:eui.Component = new eui.Component;
    private _userUI:userSub;
    private _interactionUI:interactionSub;
    private _energyUI:energyStore;
    private _helpUI:helpSub;
    private _inviteUI:invitationSub;
    private _friendUI:friendList;
    private _rankUI:rankingSub;
    private _mailUI:mailBox;
    private _plantUI:plantSub;
    private _welfareUI:welfareSub;
    private _mallUI:mallSub;
    private _wareHouseUI:wareHouse;
    private _logisticsUI:logisticsSub;
    private _otherHomeUI:otherHome;

    //
    private _bornText:string = "";

    //邀请，重点egret截图功能的使用
    private urlloader:egret.URLLoader;
    private _contMotion:egret.Sprite = new egret.Sprite;
    private _inviteBg:egret.Bitmap = new egret.Bitmap;
    private _code:egret.Sprite;
    private _rectClip:egret.Rectangle = new egret.Rectangle( 0, 0, common.stageW, common.stageH );
    private _createCode:boolean = false;

    //music
    private bgMusic:egret.Sound;
    private bgChanner:egret.SoundChannel;
    private music:eui.Rect = new eui.Rect();
    private iconMusic:eui.Image;
    private play = 0;

    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "homeSkin";
    }
    private mbtnHandler(evt:egret.TouchEvent):void{
        if(!this.load) return;
        evt.stopPropagation();
        let btn = evt.currentTarget;
        switch (btn){
            case this.head:{
                if(!this._userUI){
                    this._userUI = new userSub();
                }else{
                    this._userUI.resetExp();
                }
                this._uiFocused = this._userUI;
                break;
            }
            case this.interaction:{
                if(!this._interactionUI){
                    this._interactionUI = new interactionSub();
                }else{
                   
                }
                this._uiFocused = this._interactionUI;
                break;
            }
            case this.addEnergy:{
                if(!this._energyUI){
                    this._energyUI = new energyStore();
                }else{
                    this._energyUI.getRecordList();
                }
                this._uiFocused = this._energyUI;
                break;
            }
            case this.help:{
                if(this.pointHelp.visible){
                    this.pointHelp.visible = false;
                }
                if(!this._helpUI){
                    this._helpUI = new helpSub();
                }
                this._uiFocused = this._helpUI;
                break;
            }
            case this.friend:{
                if(!this._friendUI){
                    this._friendUI = new friendList();
                }else{
                    this._friendUI.getList();
                }
                this._uiFocused = this._friendUI;
                break;
            }
            case this.invite:{
                // if(!this._createCode){
                //     this.createCode();
                //     this._createCode = true;
                // }else{
                //     this.toInvite();
                // }
                if(!this._inviteUI){
                    this._inviteUI = new invitationSub();
                }else{
                    this._inviteUI.getInviteList();
                }
                this._uiFocused = this._inviteUI;
                break;
            }
            case this.ranking:{
                if(!this._rankUI){
                    this._rankUI = new rankingSub();
                }
                this._uiFocused = this._rankUI;
                break;
            }
            case this.mailbox:{
                if(!this._mailUI){
                    this._mailUI = new mailBox();
                }
                this._uiFocused = this._mailUI;
                break;
            }
            case this.welfare:{
                if(!this._welfareUI){
                    this._welfareUI = new welfareSub();
                }
                this._uiFocused = this._welfareUI;
                break;
            }
            case this.mall:{
                if(!this._mallUI){
                    this._mallUI = new mallSub();
                }
                this._uiFocused = this._mallUI;
                break;
            }
            case this.warehouse:{
                if(!this._wareHouseUI){
                    this._wareHouseUI = new wareHouse();
                    this._wareHouseUI.addEventListener("resetdraw",this.getPlantList,this);
                }else{
                    this._wareHouseUI.getList();
                }
                this._uiFocused = this._wareHouseUI;
                break;
            }
            case this.logistics:{
                if(!this._logisticsUI){
                    this._logisticsUI = new logisticsSub();
                }else{
                    this._logisticsUI.getList();
                }
                this._uiFocused = this._logisticsUI;
                break;
            }
        }
        this.addChild(this.maskBg);
        this._uiFocused.addEventListener("return",this.closeAllSub,this);
        this.addChild(this._uiFocused);
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.bgActionInit();
        this.inIt();
    }

    private tap = "";
    private inIt(){
        this.area.y = common.stageH - this.area.height - 25;
        this.born.mask = this.bornMask;
        this.bornGroup.visible = true;
        this.interaction.visible = true;
        this.help.visible = true;

        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{this.tap = "";},this);
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{this.tap="move";},this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this.tap !== "move"){
                this.closePlantFrame();
            }
        },this);
        
        this.head.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.interaction.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this ,true);
        this.addEnergy.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.help.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.friend.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.invite.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.ranking.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
       
        this.welfare.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.mall.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.warehouse.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.logistics.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );

        Dispatcher.ins.addEventListener("home",this.resetInit,this);
        Dispatcher.ins.addEventListener("plantList",this.draw,this);
        Dispatcher.ins.addEventListener("draw",this.getPlantList,this);
        Dispatcher.ins.addEventListener("getHarvest",this.getPlantList,this);
        Dispatcher.ins.addEventListener("wareHouseList",this.setSeedList,this);
        Dispatcher.ins.addEventListener("openOtherHome",this.openOtherHome,this);
        Dispatcher.ins.addEventListener("toast",this.openToast,this);
        Dispatcher.ins.addEventListener("interactList",this.setInteractScroll,this);
        Dispatcher.ins.addEventListener("createPet",(evt:GameEvent)=>{
            this.createPet(evt.data);
        },this);
        Dispatcher.ins.addEventListener("invite",()=>{
            if(!this._createCode){
                this.createCode();
                this._createCode = true;
            }else{
                this.toInvite();
            }
        },this);

        // plantBotany
        Dispatcher.ins.addEventListener("plantBotany",this.plantOver,this);

        //创建mask
        this.maskBg = new egret.Shape();
        this.maskBg.graphics.beginFill(0x000000,0.6);
        this.maskBg.graphics.drawRect(0,0,common.stageW,common.stageH);
        this.maskBg.graphics.endFill();
        this.maskBg.touchEnabled = true;

        //创建邀请界面
        this.notice = new eui.Image;
        this.notice.source = "share_png";
        this.notice.width = common.stageW;
        this.notice.height = common.stageH * (480 / common.stageW);
        this.notice.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closeAllSub,this);

        
        //为主界面个人信息进行赋值
        this.head.mask = this.headMask;
        this.head.source = common.userInfo.head;
        this.nickName.text = common.userInfo.nickName.length > 5 ? common.userInfo.nickName.substr(0,4)+"..." : common.userInfo.nickName;
        this.title.text = common.userInfo.title + "lv" + common.userInfo.lv;
        this.exp.text = common.userInfo.exp + "/" + common.userInfo.maxExp;
        this.project.width = (common.userInfo.exp / common.userInfo.maxExp) * 145;
        this.energy.text = common.userInfo.energy + "";
        this.born.text = "";

        //获取植物列表
        this.getPlantList();
        
        Dispatcher.ins.addEventListener("wxConfig",this.initConfig,this);
        http.getHttpServer("wxConfig");
    
        this.seedList.itemRenderer = homePlantIR;
        this.seedList.addEventListener(eui.ItemTapEvent.ITEM_TAP,(evt:eui.ItemTapEvent)=>{
           evt.stopPropagation();
           let obj = evt.itemRenderer.data;
           let dataPost = {
               "indexId":this.indexId,
               "dId":obj.Id,
               "pId":obj.PropId
           }
           socket.request("plantBotany",dataPost);
        },this);

        //获取互动列表
        socket.request("interactList");

        //创建宠物 pet
        if(common.userInfo.petId !== 0){
            this.createPet(common.userInfo.petId);
        }

        //音乐 图标创建
        this.iconMusic = new eui.Image;
        this.iconMusic.source = "music_on_png";
        this.addChild(this.iconMusic);
        this.iconMusic.x = 700;
        this.iconMusic.y = 200;
        this.iconMusic.width = 50;
        this.iconMusic.height = 50;
        this.iconMusic.anchorOffsetX = 25;
        this.iconMusic.anchorOffsetY = 25;
        EffectUtils.rotationEffect(this.iconMusic,3000);
        this.iconMusic.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this.play == 0){
                this.iconMusic.source = "music_off_png";
                this.stopMusic();
                this.play = 1;
                egret.Tween.pauseTweens(this.iconMusic);
                EffectUtils.removeRotationEffect(this.iconMusic);
            }else{
                this.iconMusic.source = "music_on_png";
                this.openMusic();
                this.play = 0;
                EffectUtils.rotationEffect(this.iconMusic,3000);
            }
        },this);

        this.bgMusic = RES.getRes("music1_mp3");
        // this.openMusic();

        this.music.addEventListener(egret.TouchEvent.TOUCH_TAP,this.openMusic,this);
        this.music.touchEnabled = true;
    }
    public openMusic():void{
        this.bgChanner = this.bgMusic.play();
    }
    public stopMusic():void{
        if(this.bgChanner){
            this.bgChanner.stop();
            this.bgChanner = null;
        }
    }
    public changeMusic():void{
        this.bgMusic = RES.getRes("music2_mp3");
    }
    private getPlantList():void{
        socket.request("plantList");
    }
    private resetInit(evt:GameEvent):void{
        let result = evt.data;
        switch(result.note){
            case "update":{
                let obj = result.data;
                common.userInfo.exp = obj.exp;
                if(common.userInfo.lv !== obj.lv){
                    common.userInfo.lv = obj.lv;
                    common.userInfo.title = obj.lvName;
                    common.userInfo.maxExp = obj.maxExp;
                    this.title.text = common.userInfo.title + "lv" + common.userInfo.lv;
                }
                this.exp.text = common.userInfo.exp + "/" + common.userInfo.maxExp;
                this.project.width = (common.userInfo.exp / common.userInfo.maxExp) * 145;
                break;
            }
            case "energy":{
                common.userInfo.energy = result.data.energy;
                this.energy.text = common.userInfo.energy + "";
                if(this._plantUI){
                    this._plantUI.setEnergy();
                }
                break;
            }
            case "harvest":{
                let _model = new model("恭喜获得第一次收获，是否去仓库查看");
                _model.addEventListener("delete",()=>{
                    this.removeChild(_model);
                    if(!this._wareHouseUI){
                        this._wareHouseUI = new wareHouse();
                    }
                    this._wareHouseUI.changePage("harvest");
                    this._uiFocused = this._wareHouseUI;
                    this.addChild(this.maskBg);
                    this._uiFocused.addEventListener("return",this.closeAllSub,this);
                    this.addChild(this._uiFocused);
                },this); 
                this.addChild(_model);
                break;
            }
        }
    }
    private openOtherHome(evt:GameEvent):void{
        let data = evt.data;
        let isFriend = data.isFriend;
        let userId = data.userId;
        if(!this._otherHomeUI){
            this._otherHomeUI = new otherHome(isFriend,userId);
        }else{
            this._otherHomeUI.getInfo(isFriend,userId);
        }
        this.closeAllSub();
        this.addChild(this._otherHomeUI);
    }
    private draw(evt:GameEvent){
        let obj = evt.data;
        if(obj.note == "otherHome") return;
        let flower = obj.list;
        common.userPlant = [];
        common.plantIndex = [0,0,0,0,0,0,0,0,0];
        //土地信息初始化
        if(this.plant.length > 0){
            for(let i = 0;i < 9;i ++){
                this.plant[i].url = "";
            }
        }else{
            for(let i = 0;i < 9;i++){
                let fl:plantInfom = new plantInfom();
                fl.Id = i;
                fl.x = this.plantIndex[i].x;
                fl.y = this.plantIndex[i].y;
                this.plant.push(fl);
                this.plant[i].addEventListener(egret.TouchEvent.TOUCH_TAP,this.plantAnaly,this);
                this.addChild(this.plant[i]);
            }
        }
        
        for(let i = 0,lg = flower.length;i < lg;i++){
            let obj = flower[i];
            common.plantIndex[obj.IndexId] = 1;
            this.plant[obj.IndexId].Id = obj.IndexId;
            this.plant[obj.IndexId].name = obj.PName;
            this.plant[obj.IndexId].url = "fl_"+obj.PId+"_"+obj.GrowStage+"_png";
            this.plant[obj.IndexId].growStage = obj.GrowStage;
            this.plant[obj.IndexId].nEx = obj.CurGrow;
            this.plant[obj.IndexId].mEx = obj.Grow3 + 1;
            this.plant[obj.IndexId].plantId = obj.Id;
            this.plant[obj.IndexId].index = i;
            common.userPlant.push(obj);
        }
        if(common.userInfo.login == 0){
            this.toGuide();
        }else if(common.userInfo.login == 1){
            this.load = true;
        }else{
            this.load = true;
            if(common.userInfo.today == 0){
                this.welfare.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
                common.userInfo.today = -1;
            }
        }
    }
    private plantAnaly(evt:egret.TouchEvent){
        evt.stopPropagation();
        let target = evt.currentTarget;
        if(target.url.length == 0){
            let _id = target.Id;//土地id
            this.indexId = _id;
            this.showSeedList(target.x,target.y);
        }else{
            this.addChild(this.maskBg);
            let _name = target.name;//没有name
            let _show = target.url;
            let _plantId = target.plantId;
            let _id = target.Id;
            let _curGrow = target.nEx;
            let _maxGrow = target.mEx;
            let _growStage = target.growStage;//当前阶段
            let _index = target.index;
            if(!this._plantUI){
                this._plantUI = new plantSub(_index,_name,_show,_id,_plantId,_curGrow,_maxGrow,_growStage);
            }else{
                this._plantUI.anayData(_index,_name,_show,_id,_plantId,_curGrow,_maxGrow,_growStage);
            }
            this._uiFocused = this._plantUI;
            this._uiFocused.addEventListener("return",this.closeAllSub,this);
            this.addChild(this._uiFocused);
        }
    }

    private closeAllSub(){
        if(this.isInvite){
            this.isInvite = false;
            this.removeChild(this.maskBg);
            this.removeChild(this.notice);
            this.maskBg.removeEventListener( egret.TouchEvent.TOUCH_TAP, this.closeAllSub, this );
            return;
        }
        this.removeChild(this.maskBg);
        this._uiFocused.removeEventListener("return",this.closeAllSub,this);
        this.removeChild(this._uiFocused);   
        this._uiFocused = null;
    }

    private resetdraw():void{
        socket.request("plantList");
        this.closeAllSub();
    }

    private openToast(evt:GameEvent){
        let data = evt.data;
        this.toast.show(data);
        this.addChild(this.toast);
    }
    private plantOver(){
        if(common.userInfo.login == 0){
            common.userInfo.login = 1;
            this.guideToPlant.visible = false;
            this.guideToDetails.visible = true;
        }
        this.plantFrame.visible = false;
        this.getPlantList();
        this.toast.show("种植成功");
        this.addChild(this.toast);
    }
    private toGuide():void{
        EffectUtils.stampEffect(this.showPoint,500,{x1:358,y1:697,x2:364,y2:704});
        EffectUtils.stampEffect(this.plantPoint,500,{x1:367,y1:564,x2:373,y2:569});
        EffectUtils.stampEffect(this.detailsPoint,500,{x1:347,y1:704,x2:355,y2:712});

        this.setChildIndex(this.guideGroup,this.numChildren-1);

        this.guideGroup.visible = true;
        this.welcome.visible = true;
        this.welcome.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            this.welcome.visible = false;
            this.guideToShow.visible = true;
        },this);
        this.toPlant.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
            evt.stopPropagation();
            this.indexId = 0;
            this.guideToShow.visible = false;
            this.guideToPlant.visible = true;
            this.showSeedList();
            this.stop.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:GameEvent)=>{
                evt.stopPropagation();
            },this);
            this.toDoPlant.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:GameEvent)=>{
                evt.stopPropagation();
                let obj = this._seedData[1];
                let dataPost = {
                    "indexId":this.indexId,
                    "dId":obj.Id,
                    "pId":obj.PropId
                }
                socket.request("plantBotany",dataPost);
            },this);
        },this);
        this.toDetails.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.guideToDetails.visible = false;
            this.guideGroup.visible = false;
            let target = common.userPlant[0];
            let _name = target.PName;//没有name
            let _show = "fl_"+target.PId+"_"+target.GrowStage+"_png";
            let _plantId = target.Id;
            let _id = target.IndexId;
            let _curGrow = target.CurGrow;
            let _maxGrow = target.Grow3 + 1;
            let _growStage = target.GrowStage;//当前阶段
            let _index = 0;
            if(!this._plantUI){
                this._plantUI = new plantSub(_index,_name,_show,_id,_plantId,_curGrow,_maxGrow,_growStage);
            }else{
                this._plantUI.anayData(_index,_name,_show,_id,_plantId,_curGrow,_maxGrow,_growStage);
            }
            this.addChild(this.maskBg);
            this._uiFocused = this._plantUI;
            this._uiFocused.addEventListener("return",this.closeAllSub,this);
            this.addChild(this._uiFocused);
            
            this.pointHelp.visible = true;
            EffectUtils.stampEffect(this.pointHelp,500,{x1:605,y1:92,x2:640,y2:64});
        },this);
    }

    private getSeedList():void{
        let dataPost:any = {};
        let type:number = 2;
        dataPost.type = 0;
        socket.request("wareHouseList",dataPost,type);
    }
    private setSeedList(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note == 2){
            let list = obj.list;
            if(list.length == 0){
                // this.toast.show("没有种子，请至商店购买");
                // this.addChild(this.toast);
                // this.plantFrame.visible = false;
               
                    let _model = new model("没有种子，是否至商店购买");
                    _model.addEventListener("delete",()=>{
                        this.removeChild(_model);
                        if(!this._mallUI){
                            this._mallUI = new mallSub();
                        }
                        this._uiFocused = this._mallUI;
                        this.addChild(this.maskBg);
                        this._uiFocused.addEventListener("return",this.closeAllSub,this);
                        this.addChild(this._uiFocused);
                    },this);
                this.addChild(_model);
                return;
            }
            this.plantFrame.visible = true;
            let arr = [];
            for(let i = 0,lg = list.length;i < lg;i ++){
                let trObj = list[i];
                trObj.icon = "fl_" + trObj.PropId + "_grow1_png";
                arr.push(trObj);
            }
            this._seedData = arr;
            this._seedArray =  new eui.ArrayCollection(arr);
            this.seedList.dataProvider = this._seedArray;
            this._seedArray.refresh();
        }
    }
    private showSeedList(x:number = 0,y:number = 0):void{
        if(x !== 0){
            this.plantFrame.anchorOffsetX = this.plantFrame.width / 2;
            this.plantFrame.anchorOffsetY = this.plantFrame.height;
            this.plantFrame.x = x;
            this.plantFrame.y = y;
        }
        if(common.userInfo.login == 0){
            this.setChildIndex(this.plantFrame,this.numChildren-2);
        }else{
            this.setChildIndex(this.plantFrame,this.numChildren-1);
        }
        this.getSeedList();
    }
    private closePlantFrame():void{
        if(this.plantFrame.visible){
            this.plantFrame.visible = false;
        }
    }
    private initConfig(evt:GameEvent):void{
        let data = evt.data;
        if(wx){
            var that = this;
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: data.appId, // 必填，公众号的唯一标识
                timestamp: data.timestamp, // 必填，生成签名的时间戳
                nonceStr: data.nonceStr, // 必填，生成签名的随机串
                signature: data.signature,// 必填，签名
                jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline','chooseWXPay'] // 必填，需要使用的JS接口列表
            });
            wx.ready(function(){
                // alert("ready");
                // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
                common.configReady = true;
                wx.getLocation({
                    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                    success: function (res) {
                        var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                        var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                        var speed = res.speed; // 速度，以米/每秒计
                        var accuracy = res.accuracy; // 位置精度
                    }
                });
                
                let obj = {
                    title:'一起农场吧', // 分享标题
                    desc: '通过网络浇水养花种植多肉，成熟以后就可以送到身边啦',// 分享描述
                    link: 'https://wechat.17pkmj.com/h5code?gameid=2001',// 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl: 'http://17pkmj.image.alimmdn.com/2001/resource/assets/loading/loading.jpg', // 分享图标
                }
                let shareApp:BodyMenuShareAppMessage = new BodyMenuShareAppMessage();
                shareApp.title = obj.title;
                shareApp.desc = obj.desc;
                shareApp.link = obj.link;
                shareApp.imgUrl = obj.imgUrl;
                wx.onMenuShareAppMessage(shareApp);
                let shareTimeline:BodyMenuShareTimeline = new BodyMenuShareTimeline();
                shareTimeline.title = obj.title;
                shareTimeline.link = obj.link;
                shareTimeline.imgUrl = obj.imgUrl;
                wx.onMenuShareTimeline(shareTimeline);

                that.openMusic();
            });
            wx.error(function(res){
                // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
            });
        }
    }
    // evt:egret.Event
    private createCode():void{
        this._code = new egret.Sprite();
        this._code = qr.QRCode.create("https://wechat.17pkmj.com/h5code?gameid=2001&invite="+common.userInfo.userId,214,214);
        this._contMotion.addChild(this._code);
        this._code.x = 272;
        this._code.y = 861;
       
        var loader:egret.ImageLoader = new egret.ImageLoader();
        loader.addEventListener(egret.Event.COMPLETE, this.loadCodeBg, this);
        var url:string = "http://17pkmj.image.alimmdn.com/2001/resource/assets/invite_bg.png";
        //开始加载
        loader.load(url);
    }
    private loadCodeBg(evt:egret.Event):void{
        var loader:egret.ImageLoader = <egret.ImageLoader>evt.target;
        var bitmapData:egret.BitmapData = loader.data;
        var texture = new egret.Texture();
        texture.bitmapData = bitmapData;
        this._inviteBg.texture = texture;
        this._contMotion.addChild(this._inviteBg);
        this._contMotion.setChildIndex(this._inviteBg,0);
        this.toInvite();
    }
    private toInvite():void{
        var rt:egret.RenderTexture = new egret.RenderTexture;
        rt.drawToTexture( this._contMotion, this._rectClip );
        let a = rt.toDataURL("image/png", this._rectClip);
    
        window["setImgSrc"](a);
        window["alertImgShow"]();
    }
    private _isLoop = false;
    private setInteractScroll(evt:GameEvent):void{
        let obj = evt.data;
        let list = obj.list;
        this._bornText = "";
        for(let i = 0;i < list.length ; i++){
            let str = list[i];
            this._bornText = this._bornText + "    " + str.NickName + "来" + str.Msg +  str.WaterCount + "次";
        }
        this.born.text = this._bornText;
        this.born.anchorOffsetX = this.born.width;
        if(!this._isLoop){
            this.loop();
        }
    }
    private loop():void{
        let width = this.born.width + 300;
        this._isLoop = true;
        this.born.x = width;
        egret.Tween.get(this.born).to({x:0},width * 25).call(()=>{
            this.loop();
        });
    }
    private bgActionInit():void{
        var data = RES.getRes("action_json");
        var txtr = RES.getRes("action_png");
        var action = new egret.MovieClipDataFactory( data, txtr );
        var windmill = new egret.MovieClip(  action.generateMovieClipData( "windmill" ) );
        windmill.x = 65;
        windmill.y = 160;
        windmill.scaleX = 0.75;
        windmill.scaleY = 0.75;
        this.addChild(windmill);
        windmill.gotoAndPlay(0,-1);
    }
    private createPet(id):void{
        //宠物
        var data = RES.getRes("my_pet_json");
        var txtr = RES.getRes("my_pet_png");
        var mcFactory:egret.MovieClipDataFactory = new egret.MovieClipDataFactory( data, txtr );
        var pet:egret.MovieClip = new egret.MovieClip( mcFactory.generateMovieClipData( "pet_"+id ) );
        this.addChild(pet);
        pet.x = 280;
        pet.y = 420;
        pet.scaleX = 0.6;
        pet.scaleY = 0.6;
        pet.gotoAndPlay( 1 ,3);
        pet.touchEnabled = true;
        pet.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let num = Math.ceil(Math.random() * 5);
            pet.gotoAndPlay( 1 ,num);
        },this);
    }
}

class homePlantIR extends eui.ItemRenderer{
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "homePlantIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
    }
    protected dataChanged():void{

    }
}